<?php
// $Id: login.php 436 2012-06-07 04:53:00Z tomqin@gmail.com $

defined('IN_ARESCMS') or exit('Access Denied!');

class Order_Action extends Action_Front{

    //网上订餐 - 购物车
    function onDingcan(){
        $this->setVar('site_desc', '网上订餐');
        if(array_get_bool($_POST, 'dosubmit')){
            $order = App::module('OrderBulider', '', array('dingcan'));
            $order->initData($_POST);
            if($error = $order->checkShop()){
                $this->failRender('无法创建订单', $error.'，请稍候再试！');
            }else{
                $verify = randomstr(8);
                App::setCookie('order_verify', $this->getVerfiy($verify));

                $this->setVar('dishes', $order->getDishes());
                $this->setVar('shop',   $order->getShopInfo());
                $this->setVar('banks',  Alipay::getUnionBanks());
                $this->setVar('verify', $verify);
                $this->render('user/order_dingcan');
            }
        }else{
            header('Location: /');
            exit;
        }
    }

    //网上订餐 - 支付
    function onPayment(){
        $this->setVar('site_desc', '网上订餐');

        if(array_get_bool($_POST, 'dosubmit') &&
           App::getCookie('order_verify') == $this->getVerfiy(array_get_string($_POST, 'verify'))){
            $order = App::module('OrderBulider', '', array('dingcan'));
            $order->initData($_POST);
            if($error = $order->checkShop()){
                $this->failRender('无法创建订单', $error.'，请稍候再试！');
            }else{
                if(!$order->createOrder()){ //
                    $this->failRender('订单创建失败', '由于系统原因，订单创建失败，请稍候再试！');
                }else{
                    //删除cookie;
                    App::setCookie('order_verify', null);

                    //支付
                    $alipay = new Alipay(Alipay::getConfig());
                    $orderArr = array(
                        'order_code'   => $order->getOrderSn(),
                        'order_title'  => $order->getOrderTitle(),
                        'order_desc'   => $order->getOrderDesc(),
                        'order_amount' => rand(0,39)/100, //$order->getSumPrice(),
                        'pay_method'   => 2,
                        'pay_bank'     => array_get_string($_POST, 'pay_bank'),
                    );
                    $pay_code = $alipay->getPaymentCode($orderArr);

                    $this->setVar('shop', $order->getShopInfo());
                    $this->setVar('sum_price', $order->getSumPrice());
                    $this->setVar('order_sn', $order->getOrderSn());
                    $this->setVar('pay_code', $pay_code);
                    $this->setVar('banks',  Alipay::getUnionBanks());
                    $this->render('user/order_checkout');
                }
            }
        }else{
            $this->failRender('订单已过期', '订单已过期，请返回重试！');
        }
    }

    function onAlipay(){
        p($_POST);
        p($_GET);
    }


    //叫外卖 - 购物车
    function onWaimai(){
        $this->setVar('site_desc', '外卖订单');
        if(array_get_bool($_POST, 'dosubmit')){
            $order = App::module('OrderBulider', '', array('waimai'));
            $order->initData($_POST);
            if($error = $order->checkShop()){
                $this->failRender('无法创建订单', $error.'，请稍候再试！');
            }else{
                $verify = randomstr(8);
                App::setCookie('order_verify', $this->getVerfiy($verify));

                $this->setVar('dishes', $order->getDishes());
                $this->setVar('shop',   $order->getShopInfo());
                $this->setVar('verify', $verify);
                $this->render('user/order_waimai');
            }
        }else{
            header('Location: /');
            exit;
        }
    }

    //叫外卖 - 购物车
    function onDone(){
        $this->setVar('site_desc', '外卖订单');

        if(array_get_bool($_POST, 'dosubmit') &&
           App::getCookie('order_verify') == $this->getVerfiy(array_get_string($_POST, 'verify'))){
            $order = App::module('OrderBulider', '', array('waimai'));
            $order->initData($_POST);
            if($error = $order->checkShop()){
                $this->failRender('无法创建订单', $error.'，请稍候再试！');
            }else{
                if(!$order->createOrder()){ //
                    $this->failRender('订单创建失败', '由于系统原因，订单创建失败，请稍候再试！');
                }else{
                    //删除cookie;
                    App::setCookie('order_verify', null);

                    $this->sendWaimaiSMS($order);

                    $this->setVar('shop', $order->getShopInfo());
                    $this->setVar('sum_price', $order->getSumPrice());
                    $this->setVar('order_sn', $order->getOrderSn());
                    $this->render('user/order_success');
                }
            }
        }else{
            $this->failRender('订单已过期', '订单已过期，请返回重试！');
        }
    }

    private function failRender($desc, $title){
        $this->setVar('site_desc', $desc);
        $this->setVar('fail_title', $title);
        $this->render('user/order_failed');
        exit;
    }

    private function getVerfiy($str){
        return md5($str . App::getConfig('system.auth_key') . $_SERVER['HTTP_USER_AGENT']);
    }

    private function sendDingcanSMS(OrderBulider_Module $order){
        $msg_sign = '-速订客外卖网(sudingke.com)';

        //给订餐者
        $tel  = $order->getMobile();
        $txt  = '{realname},{orderdesc},备注:{$memo}'.$msg_sign;

        //发送短信
        //echo $tel, " = ", $txt, "<hr />\n";
        $ret = $sms->sendSMS(array($tel), $txt);
    }

    private function sendWaimaiSMS(OrderBulider_Module $order){
        $msg_sign = '-速订客外卖网(sudingke.com)';

        //给订餐者
        $sms  = '{realname},{orderdesc}'.$msg_sign;
        $var[$order->getMobile()] = $sms;

        //给外卖公司
        $sms = '{orderdesc}.联系人:{realname},{mobile}/{telphone};地址:{address};备注:{memo}'.$msg_sign;
        $shopInfo = $order->getShopInfo();
        $var[$shopInfo['sms_number']] = $sms;

        //发送短信
        $sms = new SMS;
        foreach($var as $tel => $txt){
            $txt = preg_replace("/\s+/i", "", $order->genMessage($txt));
            //echo $tel, " = ", $txt, "<hr />\n";
            $ret = $sms->sendSMS(array($tel), $txt);
        }
    }
}
?>